package com.definity.toolkit.ui.style;

import java.awt.Color;
import java.awt.Font;
import java.awt.Paint;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.Border;

public class Style {

	public static final int LEFT = SwingConstants.LEFT;
	public static final int CENTER = SwingConstants.CENTER;
	public static final int RIGHT = SwingConstants.RIGHT;

	private String id;
	private Boolean opaque;
	private Font font;
	private Paint color;
	private Color background;
	private Color foreground;
	private Border border;
	private Integer alignment;

	public Style(String id) {
		this.id = id;
	}

	public final Paint color() {
		return color;
	}
	
	public final Style color(Paint color) {
		this.color = color;
		return this;
	}
	
	public final Boolean opaque() {
		return opaque;
	}
	
	public final Style opaque(Boolean opaque) {
		this.opaque = opaque;
		return this;
	}
	
	public final Color background() {
		return background;
	}
	
	public final Style background(Color background) {
		this.background = background;
		return this;
	}
	
	public final Color foreground() {
		return foreground;
	}

	public final Style foreground(Color foreground) {
		this.foreground = foreground;
		return this;
	}

	public final Font font() {
		return font;
	}

	public final Style font(Font font) {
		this.font = font;
		return this;
	}

	public final String id() {
		return id;
	}

	public final Border border() {
		return border;
	}

	public final Style border(Border border) {
		this.border = border;
		return this;
	}

	public final Integer alignment() {
		return alignment;
	}

	public final Style alignment(int alignment) {
		switch (alignment) {
			case LEFT:
			case CENTER:
			case RIGHT:
				this.alignment = alignment;
				break;
			default:
				throw new RuntimeException("Use the constants: Style.LEFT, Style.CENTER or Style.RIGHT");
		}
		return this;
	}

	public void apply(JComponent component) {
		if (font() != null) {
			component.setFont(font());
		}
		if (opaque() != null)
			component.setOpaque(opaque());
		if (border() != null) {
			component.setBorder(border());
		}
		if (foreground() != null) {
			component.setForeground(foreground());
		}
		if (background() != null) {
			component.setBackground(background());
		}
		if (component instanceof JTextField) {
			JTextField textField = (JTextField) component;
			if (alignment() != null) {
				textField.setHorizontalAlignment(alignment());
			}
		} else if (component instanceof JLabel) {
			JLabel label = (JLabel) component;
			if (alignment() != null) {
				label.setHorizontalAlignment(alignment());
			}
		}
	}
	
}
